const express = require('express');
const axios = require('axios');
const bodyParser = require('body-parser');
const cors = require('cors');

const app = express();
app.use(bodyParser.json());
app.use(cors());

const events = [];

app.post('/events', async (req, res) => {
    const event = req.body;
    events.push(event);
    try {
        await axios.post('http://posts-srv:4000/events', event);
        await axios.post('http://comments-srv:4001/events', event);
        await axios.post('http://query-srv:4002/events', event);
        await axios.post('http://moderation-srv:4003/events', event);

        console.log('Receive event:', event);
    }
    catch (e) {
        console.log(e);
    }
    res.status(200).send({});
});

app.get('/events', (req, res) => {
    res.send(events);
});

app.listen(4005, () => {
    console.log('v1.2');
    console.log('Listening on 4005');
});